package cn.gbase.jiangsu.data.transfer.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Scope;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import cn.gbase.jiangsu.data.transfer.bean.SupplierCount;
import cn.gbase.jiangsu.data.transfer.bean.SupplierUser;
import cn.gbase.jiangsu.data.transfer.service.SupplierCaiWuService;
import cn.gbase.jiangsu.data.transfer.util.PropertyFilter;
import cn.gbase.jiangsu.data.transfer.util.SystemContext;

@IocBean
@At("/supplierCaiWu")
public class SupplierCaiWuAction {
	@Inject
	private SupplierCaiWuService supplierCaiWuService;

	@At("/countMoney")
	@Ok("jsp:/supplierCw/supplierCount")
	public Object countMoney(HttpServletRequest request,
			@Attr(scope = Scope.SESSION, value = SystemContext.SUPPLIERUSER) SupplierUser supplierUser) {
		List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);
		Cnd cnd = PropertyFilter.toCnd(filters, "id", "DESC");
		BigDecimal yingskCount = supplierCaiWuService.yingskSupplierCount(supplierUser, cnd);
		BigDecimal yiskCount = supplierCaiWuService.yiskSupplierCount(supplierUser, cnd);
		SupplierCount supplierCount = new SupplierCount();
		supplierCount.setYingskCount(yingskCount);
		supplierCount.setYiskCount(yiskCount);
		return supplierCount;
	}
	
	/**
	 * 分页查询账目（账目管理页面）.
	 * 
	 * @param currentPage
	 * @param groupId
	 * @param addTime
	 * @param keyword
	 * @param req
	 * @throws UnsupportedEncodingException
	 */
	@At("/listAccount")
	@Ok("jsp:/supplierCw/listSupplierAccounts")
	public QueryResult listSupplierAccounts(@Param("p") int currentPage,
			@Param("accountPeriod") String accountPeriod,
			@Param("supplierId") int supplierId, HttpServletRequest request, HttpSession session) {
		SupplierUser u = (SupplierUser) session.getAttribute("supplierUser");
		
		// 回传表单输入
		Map<String, String> itemMap = new HashMap<String, String>();
		itemMap.put("accountPeriod", accountPeriod);
		itemMap.put("supplierId", "" + u.getSupplierId());
		request.setAttribute("formdata", itemMap);


		return supplierCaiWuService.getSupplierAccountsList(currentPage,
				accountPeriod, u.getSupplierId());
	}
}
